import scrapy


class BiliSpider(scrapy.Spider):
    name = "bili"
    allowed_domains = ["bilibili.com"]
    start_urls = ["https://search.bilibili.com/all?vt=04488602&keyword=%E7%9F%A5%E8%AF%86%E5%9B%BE%E8%B0%B1"]
    """
    添加custom_settings属性
    FEED_FORMAT:指定输出格式为JSON
    FEED_URI:指定输出文件的路径和名称
    """
    custom_settings = {
        'FEED_FORMAT': 'json',
        'FEED_URI': 'output.json'
    }

    def parse(self, response):
        list_path = response.css("div.bili-video-card__wrap.__scale-wrap")
        for path in list_path:
            title = path.css("div > div > a > h3::text").getall()
            name = path.css("div > div > p > a > span.bili-video-card__info--author::text").getall()

            yield {
                'title': title,
                'name': name
            }
